package com.chixing.day12.Date;
/**
 * DateApi
 * localDate (日期)包含年月日
 * localTime (时间)包含时分秒
 * localDateTime (包含年月日,时分秒)日期时间
 * DateTimeFormatter:日期时间格式化类
 * Instant:时间戳，表示一个特定的时间瞬间
 * Duration:用于计算2个时间(LocalTime,时分秒)的距离
 * Period:用于计算2个日期(LocalDate,年月日)的距离
 * ZonedDateTime:包含时区的时间
 */

import java.time.*;

import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateDemo {
    public  static <DateTimeFormater> void main(String[] args){
        Date date = new Date();
        //区分调用两个包中Date日期
        Date dat1;
        java.sql.Date dat2;

        System.out.println("=========LocalDate============");
        //创建指定日期
        LocalDate date1 = LocalDate.of(1985,10,15);
        System.out.println("指定的时间是: " + date1);
        //得到当前日期
        LocalDate now = LocalDate.now();
        System.out.println("当前日期: " + now);
        //获得日期信息
        System.out.println("年: " + now.getYear());
        System.out.println("月: " + now.getMonthValue());
        System.out.println("日: " + now.getDayOfMonth());
        System.out.println("星期: " + now.getDayOfWeek());
        System.out.println("一年中的第: " + now.getDayOfYear() + " : 天 ");

        System.out.println("=========LocalTime============");

        LocalTime time = LocalTime.of(9,54,32,129_900_000);
        System.out.println("指定时间是: " + time);
        LocalTime now1 = LocalTime.now();
        System.out.println("当前时间: " + now1);
        System.out.println("小时: " + now1.getHour());
        System.out.println("分钟: " + now1.getMinute());
        System.out.println("秒: " + now1.getSecond());
        System.out.println("纳秒:" + now1.getNano());

        System.out.println("=========LocalDateTime============");

        LocalDateTime dateTime = LocalDateTime.of(2018,9,23,10,1,00,730);
        System.out.println("指定日期时间是: " + dateTime);
        LocalDateTime now2 = LocalDateTime.now();
        System.out.println("当前的日期时间为: " + now2);
        System.out.println("年 ： " + now2.getYear());
        System.out.println("月 : " + now2.getMonthValue());
        System.out.println("日 ：" + now2.getDayOfMonth());
        System.out.println("小时 ： " + now2.getHour());
        System.out.println("分钟 ： " + now2.getMinute());
        System.out.println("秒 : " + now2.getSecond());
        System.out.println("纳秒 : " + now2.getNano());

        System.out.println("=========LocalDateTimeUpdate============");

        LocalDateTime now3 =  LocalDateTime.now();
        System.out.println("当前系统日期时间: " + now3);
        LocalDateTime year = now3.withYear(2071);
        System.out.println("修改的年份是: " + year);
        System.out.println("now == year : "+ (now3 == year));
        System.out.println("修改的月份 ： " + now3.withMonth(9));
        System.out.println("修改的日 ：" + now3.withDayOfMonth(28));
        System.out.println("修改的小时  ： " + now3.withHour(12));
        System.out.println("修改的分钟 ：" + now3.withMinute(20));
        System.out.println("修改的秒 ： " + now3.withSecond(45));
        System.out.println("修改的纳秒 ： " + now3.withNano(750));

        System.out.println("=========在当前对象上加上或减去指定的时间==");

        LocalDateTime localDateTime = now3.plusDays(5);
        System.out.println("5天后 ： " + localDateTime);
        System.out.println("10年后 ： " + now3.plusYears(10));
        System.out.println("20个月后 ： " + now3.plusMonths(20));

        System.out.println("20年前: " + now3.minusYears(20));
        System.out.println("5个月前: " + now3.minusMonths(5));
        System.out.println("100天前: " + now3.minusDays(100));

        System.out.println("=========两个日期，可直接进行比较====");

        LocalDate now4 = LocalDate.now();
        LocalDate of = LocalDate.of(2019, 8, 23);
        System.out.println(now4.equals(of)); //false
        System.out.println(now4.isAfter(of)); //true
        System.out.println(now4.isBefore(of)); //false

        System.out.println("=====日期格式化============");

        LocalDateTime now5 = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String dateTime1 = now5.format(formatter);
        System.out.println("格式化后的时间日期: " + dateTime1);
        LocalDateTime parse = LocalDateTime.parse("2024-09-04 08:29:13",formatter);
        System.out.println("解析后的日期时间: " + parse);

        System.out.println("=============instant时间戳===================");

        Instant now6 = Instant.now();
        System.out.println("当前时间戳: " + now6);
        System.out.println(now6.getNano());
        System.out.println(now6.getEpochSecond());
        System.out.println(now6.toEpochMilli());
        System.out.println(System.currentTimeMillis());
        Instant instant = Instant.ofEpochSecond(5);
        Instant instant1 = Instant.ofEpochMilli(5);
        System.out.println(instant1);
        System.out.println(instant);

        System.out.println("=======时间差=========");

        System.out.println("=====Duration===");
        LocalTime now7 = LocalTime.now();
        LocalTime time3 = LocalTime.of(14, 15, 20);
        Duration d = Duration.between(now7,time3);
        System.out.println("相差的天数: " + d.toDays());
        System.out.println("相差的小时数: " + d.toHours());
        System.out.println("相差的分钟: " + d.toMinutes());
        System.out.println("相差的秒数: " + d.toMillis());

        System.out.println("=====Period===");
        LocalDate date2 = LocalDate.now();
        LocalDate date3 = LocalDate.of(2022, 12, 20);
        Period period = Period.between(date2, date3);
        System.out.println("相差的年: " + period.getYears());
        System.out.println("相差的月: " + period.getMonths());
        System.out.println("相差的日: " + period.getDays());

    }
}
